package com.example.myapplication;

import android.graphics.Canvas;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.Date;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.TimeZone;

public class NoteArrayListAdapter extends RecyclerView.Adapter<NoteArrayListAdapter.NoteViewHolder> {
  ArrayList<Note> noteData;
  ArrayList<Note> selectedItems;
  int convertViewId;
  View mainView;
  boolean isActionShown = false;
  int actionPosition = -1;
  NoteViewFragment noteViewFragment;
  boolean isMultiSelectMode = false;
  boolean isAllSelected = false;


  public NoteArrayListAdapter(ArrayList data) {
    noteData = new ArrayList<>(data);
    selectedItems = new ArrayList<>();
    convertViewId = R.layout.single_note_item_layout;

  }

  public NoteArrayListAdapter(ArrayList<Note> noteData, int convertViewId) {
    this.noteData = noteData;
    this.convertViewId = convertViewId;
    selectedItems = new ArrayList<>();
  }

  public NoteArrayListAdapter(ArrayList<Note> noteData, NoteViewFragment noteViewFragment) {
    selectedItems = new ArrayList<>();
    this.noteData = new ArrayList<>(noteData);
    convertViewId = R.layout.single_note_item_layout;
    this.noteViewFragment = noteViewFragment;
  }

  @NonNull @Override
  public NoteViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {


    if (mainView == null) {
      mainView=LayoutInflater.from(parent.getContext()).inflate(convertViewId, parent, false);
    }
    View view=LayoutInflater.from(parent.getContext()).inflate(convertViewId, parent, false);



    return new NoteViewHolder(view);
  }

  @Override public void onBindViewHolder(@NonNull NoteViewHolder holder, int position) {
    Note note = noteData.get(position);
    holder.titleButton.setText(note.getNoteTitle());
    if (position == 0 || !IsSameDay(note.getCreateTime(),
        noteData.get(position - 1).getCreateTime())) {
      holder.dateLayout.setVisibility(View.VISIBLE);
      SetDateInfo(holder,note.getCreateTime());
    } else {
      holder.dateLayout.setVisibility(View.INVISIBLE);

    }
    holder.checkBox.setChecked(isAllSelected ? true : false);
    holder.checkBox.setVisibility(isMultiSelectMode ? View.VISIBLE : View.INVISIBLE);

  }

  @Override public int getItemCount() {
    return noteData.size();
  }

  private boolean IsSameDay(Date date1, Date date2) {
    //错误实现，此类实现是判断Date类型变量是否是同一时间点
    //if (date1.equals(date2)) {
    //  return true;
    //}
    //return false;
    Calendar calendar1 = Calendar.getInstance();
    Calendar calendar2 = Calendar.getInstance();
    calendar1.setTime(date1);
    calendar2.setTime(date2);
    if (calendar1.get(Calendar.YEAR) == calendar2.get(Calendar.YEAR)
        && calendar1.get(Calendar.MONTH) == calendar2.get(Calendar.MONTH)
        && calendar1.get(Calendar.DAY_OF_MONTH) == calendar2.get(Calendar.DAY_OF_MONTH)) {
      return true;
    }
    return false;
  }

  private void SetDateInfo(NoteViewHolder holder, Date date) {

    if (date == null) {
      return;
    }
    TimeZone timeZone = TimeZone.getTimeZone("Asia/Shanghai");
    Calendar calendar = Calendar.getInstance(timeZone);
    calendar.setTime(date);
    int year = calendar.get(Calendar.YEAR);
    int month = calendar.get(Calendar.MONTH)+1;
    int day = calendar.get(Calendar.DAY_OF_MONTH);
    holder.createYearText.setText(
        mainView.getContext().getString(R.string.date_info, String.valueOf(year)));
    holder.createMonthText.setText(
        mainView.getContext().getString(R.string.month_info, String.valueOf(month)));
    holder.createDayText.setText(
        mainView.getContext().getString(R.string.date_info, String.valueOf(day)));


  }

  public void ShowAction(int position) {
    isActionShown = true;
    actionPosition = position;
    notifyDataSetChanged();

  }

  public void HideActions() {
    isActionShown = false;
    actionPosition = -1;
    notifyDataSetChanged();
  }

  public void DrawActions(Canvas c, RecyclerView.ViewHolder viewHolder, float dX) {
    if (isActionShown && viewHolder.getAdapterPosition() == actionPosition) {
      //使用Canvas绘制

    }
  }
  public class NoteViewHolder extends RecyclerView.ViewHolder {
    //在这里定义ViewHolder的视图元素,可以实现内部find组件直接赋值
    public Button titleButton;
    public LinearLayout dateLayout;
    public TextView createYearText;
    public TextView createMonthText;
    public TextView createDayText;
    public CheckBox checkBox;


    public NoteViewHolder(View itemView) {
      super(itemView);
      //给内部组件赋值
      titleButton = itemView.findViewById(R.id.title_button);
      dateLayout=itemView.findViewById(R.id.date_layout);
      createYearText=itemView.findViewById(R.id.year_text);
      createMonthText = itemView.findViewById(R.id.month_text);
      createDayText = itemView.findViewById(R.id.day_text);
      checkBox = itemView.findViewById(R.id.checkbox);
      itemView.setOnClickListener(new View.OnClickListener() {
        @Override public void onClick(View v) {

          //HideActions();
        }
      });

      if (isMultiSelectMode == false) {
        checkBox.setChecked(false);

      }
      checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
          if (isChecked) {
            AddSelectionItem(noteData.get(getAdapterPosition()));
            if (selectedItems.size() == noteData.size()) {
              isAllSelected = true;
            }
          } else {
            isAllSelected = false;
            RemoveSelectionItem(noteData.get(getAdapterPosition()));
          }

        }
      });
      titleButton.setOnClickListener(new View.OnClickListener() {
        @Override public void onClick(View v) {
          if (isMultiSelectMode) {
            checkBox.setChecked(!checkBox.isChecked());
            return;
          }
          Note note = GetSelectedItem(getAdapterPosition());
          if (noteViewFragment != null) {
            noteViewFragment.JumpToNote(NoteViewFragment.NoteOperation.OPEN_CURRENT_NOTE, note);
          }

        }
      });

    }


  }

  public void UpdateList(ArrayList<Note> newData) {
    noteData.clear();
    noteData.addAll(newData);
    notifyDataSetChanged();
  }

  public Note GetSelectedItem(int actionPosition) {
    return noteData.get(actionPosition);
  }

  public void SetMultiSelectMode(boolean mode) {
    isMultiSelectMode = mode;
    notifyDataSetChanged();

  }

  public ArrayList<Note> GetMultiSelectedItem() {
    return selectedItems;
  }

  public void AddSelectionItem(Note note) {
    selectedItems.add(note);
    noteViewFragment.UpdateSelection(selectedItems.size());
  }
  public void RemoveSelectionItem(Note note)
  {
    selectedItems.remove(note);
    noteViewFragment.UpdateSelection(selectedItems.size());
  }

  public void SelectOrDeselectAll() {
    isAllSelected = !isAllSelected;
    notifyDataSetChanged();
  }
}
